﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace ClueOnline
{

    static class Program
    {
        public static List<Thread> clientThreads = new List<Thread>();
        public static Thread serverThread;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            string line;
            string[] command;
            string args;
            char[] delimeters = {'\t',' '};
            Console.WriteLine("Enter commands (press CTRL+Z to exit):");
            Console.WriteLine();
            while ((line = Console.ReadLine()) != null)
            {   
                Console.WriteLine("ECHO: " + line);
                command = line.Split(delimeters);
                try
                {
                    switch(command[0])
                    {
                        case "SERVER":                        
                            //Console.WriteLine("IP: {0}", command[1]);
                            serverThread = new Thread(Server.StartListening);
                            serverThread.Start();
                            break;
                        case "CLIENT":
                            //Console.WriteLine("IP: {0}", command[1]);
                            clientThreads.Add(new Thread(Client.StartClient));
                            clientThreads[clientThreads.Count-1].Start();
                            break;
                        case "STA":
                            
                            Server.Broadcast("STA PARAM1 PARAM2");
                            Server.SendToClient(SOCKET, "STA PARAM1 PARAM2");
                            break;
                        default:
                            // throw a proper error here instead of just writing out.
                            Console.WriteLine("Expected a command to send. Please type a valid command to send");
                            break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0}",ex);
                }
            }        

            Console.WriteLine("end");
        }
    }
}
